<script setup lang="tsx">
import { ref, unref } from 'vue'
import { useTable } from '@/hooks/web/useTable'
import { deleteDataApi, getListApi } from '@/api/transactionCurrency'
import { useEventBus } from '@/hooks/event/useEventBus'
import { ActionButtonsTemplate, SchemaConfig } from '@/api/global/types'
import useSchema from '@/views/Warehouse/TransactionCurrency/components/hooks/useSchema'
import { AllSchemas, useCrudSchemas } from '@/hooks/web/useCrudSchemas'
import { Search } from '@/components/Search'
import { ContentWrap } from '@/components/ContentWrap'
import { Table } from '@/components/Table'
import { getFieldEnumApi } from '@/api/transactionCurrency'
import { FieldEnum } from '@/api/transactionCurrency/types'
import { CurdData } from '@/api/transactionCurrency/types'
import { useI18n } from '@/hooks/web/useI18n'
import { BaseButton } from '@/components/Button'

defineOptions({
  name: 'TransactionCurrency'
})
const { push } = useRouter()
const { t } = useI18n()
const ids = ref<string[]>([])

const searchParams = ref({})

const setSearchParams = (params: any) => {
  searchParams.value = params
  getList()
}

const { tableRegister, tableState, tableMethods } = useTable({
  immediate: true,
  currentPageSize: 15,
  fetchDataApi: async () => {
    const { currentPage, pageSize } = tableState

    const res = await getListApi({
      pageIndex: unref(currentPage),
      pageSize: unref(pageSize),
      ...unref(searchParams)
    })
    return {
      list: res.data.list,
      total: res.data.total
    }
  },
  fetchDelApi: async () => {
    const res = await deleteDataApi(unref(ids))
    return !!res
  }
})
const { loading, dataList, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

useEventBus({
  name: 'getList',
  callback: (type: string) => {
    if (type === 'add') {
      currentPage.value = 1
    }
    getList()
  }
})

const action = (row: CurdData, type: string) => {
  push(`/warehouse/transaction-currency/${type}?id=${row.id}`)
}
const actionButtonsTemplate: ActionButtonsTemplate = {
  length: 3,
  template: (data: any) => {
    return (
      <>
        <BaseButton size="small" onClick={() => action(data.row, 'edit')}>
          {t('action.edit')}
        </BaseButton>
      </>
    )
  }
}

const fieldEnum = ref<FieldEnum>({}) // 根据实际数据初始化

const allSchemas = ref<AllSchemas>({
  searchSchema: [], // 确保与接口匹配
  tableColumns: [] // 确保与接口匹配
})

getFieldEnumApi({ field: 'all' })
  .then((res) => {
    fieldEnum.value = res.data.list
    const schemaConfig: SchemaConfig = {
      actionButtonsTemplate,
      fieldEnum: fieldEnum.value
    }

    const { crudSchemas } = useSchema(schemaConfig)
    const { allSchemas: newSchemas } = useCrudSchemas(crudSchemas)

    // 合并新的数据，确保不改变原有数据的类型
    allSchemas.value = {
      searchSchema: newSchemas.searchSchema || [],
      tableColumns: newSchemas.tableColumns || []
    }
  })
  .catch((error) => {
    console.error('API call failed', error)
  })
</script>

<template>
  <ContentWrap>
    <Search :schema="allSchemas.searchSchema" @search="setSearchParams" @reset="setSearchParams" />

    <Table
      v-model:pageSize="pageSize"
      v-model:currentPage="currentPage"
      :columns="allSchemas.tableColumns"
      :data="dataList"
      :loading="loading"
      :pagination="{
        total: total
      }"
      @register="tableRegister"
    />
  </ContentWrap>
</template>
